<?php
//页面显示
class BaseAction extends Action {	
	var $channelId;
	
	function _initialize(){	}
	
	function checkAdmin(){
		$this->assign("site_opts",$this->site);
		$adminId	=	$_SESSION['SociaxAdmin'];
		if($adminId==0 AND MODULE_NAME!='Public'){
			redirect(__ROOT__.'/index.php?app=admin&mod=Public&act=login');
		}
		
		$admin_array	=	array(9,1,8,10,11,12,56,16,19,20);
		$uid	=	intval($_SESSION['mid']);

		$isAdmin	=	Model('UserGroup')->isAdmin($uid);
		if(!$isAdmin){
			$this->error('您不是管理员！');
		}		
	}
	
	function echoRes($res, $msg='操作'){
		if($res){
			$this->success($msg.'成功');
		}else{
			$this->error($msg.'失败');
		}			
	}	
	
	function _EchoRes( $res ){
		if($res) echo 1;
		else     echo 0;
	}	
	
	function _initChannelId(){
		$channel = $_REQUEST['channelId'];
		$this->channelId = isNumber($channel)?
								$channel:
								D('Channel')->where("domain='".$channel."'")->getField('id');
		if(empty($this->channelId)){
			$this->error('频道参数不能为空。');
		}
		
		$status = D('Channel')->where("id='".$this->channelId."'")->getField('status');
		if($status!=1){
			$this->error('该频道还没开通。');
		}
		
		$this->assign('channelId', $this->channelId);		
	}	
	
	function admin_tree(){
		$categoryId = $_REQUEST['categoryId'];
		if($categoryId){
			$map['categoryId'] = $categoryId;
		}else{
			$cateArr = $this->getCategory(2);
			$this->assign('cateArr', $cateArr);
		}
		$map['channelId'] = $this->channelId;
		//$map['pageId']    = array('exp', 'is not null');
		
		$pageList = D('Page', 'portal')->where($map)->field('id,channelId,name,pageType,jumpURL,jumpTarget,readerCount,categoryId')->findall();
		if(empty($categoryId)){
			foreach ($pageList as $p){
				$cateId = $p['categoryId'];
				$res[ $cateId ][] = $p;
			}			
		}else{
			$res = $pageList;
		}

		$this->assign( 'pageList', $res );
		return $res;
	}
	
	function _myChannel(){		
		$uid    = $this->mid;
		$myChannel = D('')->query("SELECT * FROM ".C("DB_PREFIX")."portal_purview WHERE uid='$uid' AND channelId IN
		                          (SELECT id FROM ".C("DB_PREFIX")."portal_channel WHERE status=1)");
		//$myChannel = D('Purview', 'portal')->where($map)->findall();	
		$this->assign( 'myChannel', $myChannel );
	}
	
	
	
	function _getOrder(){
		//排序字段 默认为主键名
		if(isset($_REQUEST['order'])) {
			$order = $_REQUEST['order'];
		}else {
			$order = !empty($sortBy)? $sortBy: 'id';
		}

		//排序方式默认按照倒序排列
		//接受 sost参数 0 表示倒序 非0都 表示正序
		if(isset($_REQUEST['sort'])) {
			$sort = $_REQUEST['sort']?'asc':'desc';
		}else {
			$sort = $asc?'asc':'desc';
		}	
		
		$res = $order.' '.$sort;
		
		//列表排序显示
		$sortImg    = $sort ;                                   //排序图标
		$sortAlt    = $sort == 'desc'?'升序排列':'倒序排列';    //排序提示
		$sort       = $sort == 'desc'? 1:0;                     //排序方式
		$this->assign('sort',       $sort);
		$this->assign('order',      $order);
		$this->assign('sortImg',    $sortImg);
		$this->assign('sortType',   $sortAlt);

		return 	$res;
	}	
	
	function _isAllow(){
		$isAdmin = model('UserGroup')->isAdmin($this->mid);
		if($isAdmin){
			$userLevel = 3;
			$this->assign('userLevel', $userLevel);
			return $userLevel;
		}
		
		$map['channelId'] = $this->channelId;		
		$map['uid']       = $this->mid;			
		$purview = D('Purview', 'portal')->where($map)->findall();

		if(empty($purview)){
			$this->error('您无权管理该频道');
		}	
		$userLevel = 1;
		foreach ($purview as $v){
			if(empty($v['categoryId'])) $userLevel = 2;
		}
		$this->assign('userLevel', $userLevel);
		
		return $userLevel;	
	}
	
	function getCategory($userLevel=1){
		$map['channelId'] = $this->channelId;
		if($userLevel==1){ 
			$map['uid']       = $this->mid;
			$list = D('Purview', 'portal')->where($map)->field('categoryId')->findall();
			foreach ($list as $v){
				if($v['categoryId']) $cateId[] = $v['categoryId']; 
			}
			unset($map['uid']);
			$map['id'] = array('in', $cateId);
		}else{
			$cateArr[0] = '未分层';
		}
		$list = D('PageCategory')->where($map)->findall();		
		foreach ($list as $vo){
			$cateArr[ $vo['id'] ] = $vo['title'];
		}
		
		$this->assign('cateArr', $cateArr);
		return $cateArr;
	}
}
?>